﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="YubicoClientAdapter.cs" company="Sven Erik Matzen">
//   (c) 2013 Sven Erik Matzen
// </copyright>
// <summary>
//   Defines the YubicoClientAdapter type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Sem.Authentication.MvcHelper.Yubico.Client
{
    using System.Diagnostics.CodeAnalysis;

    using YubicoDotNetClient;

    /// <summary>
    /// This class does add an abstraction layer to the sealed <see cref="YubicoClient"/>, so we can
    /// write unit tests without using the real client implementation. See <see cref="http://en.wikipedia.org/wiki/Adapter_pattern"/>.
    /// </summary>
    [ExcludeFromCodeCoverage]

    public class YubicoClientAdapter : IYubicoClient
    {
        /// <summary>
        /// Gets or sets the client id that you did get from YUBICO to access the server API.
        /// </summary>
        public string ClientId { get; set; }

        /// <summary>
        /// Gets or sets the API key that you did get from YUBICO to access the server API.
        /// </summary>
        public string ApiKey { get; set; }

        /// <summary>
        /// Gets or sets the sync level you want to use when accessing the server API - see 
        /// YUBICO documentation for more information.
        /// </summary>
        public string SyncLevel { get; set; }

        /// <summary>
        /// Calls the verify method of the client implementation - see YUBICO documentation for more information.
        /// </summary>
        /// <param name="onetimePassword"> The One Time Password generated by the YUBIKEY. </param>
        /// <returns> The <see cref="IYubicoResponse"/>. </returns>
        [ExcludeFromCodeCoverage]
        public IYubicoResponse Verify(string onetimePassword)
        {
            var client = new YubicoClient(this.ClientId);
            client.SetApiKey(this.ApiKey);
            client.SetSync(this.SyncLevel);
            return client.Verify(onetimePassword);
        }
    }
}